<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateUserUploadAPIRequest;
use App\Http\Requests\API\UpdateUserUploadAPIRequest;
use App\Models\UserUpload;
use App\Repositories\UserUploadRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class UserUploadController
 * @package App\Http\Controllers\API
 */

class UserUploadAPIController extends AppBaseController
{
    /** @var  UserUploadRepository */
    private $userUploadRepository;

    public function __construct(UserUploadRepository $userUploadRepo)
    {
        $this->userUploadRepository = $userUploadRepo;
    }

    /**
     * @param Request $request
     * @return Response
     *
     * @SWG\Get(
     *      path="/userUploads",
     *      summary="Get a listing of the UserUploads.",
     *      tags={"UserUpload"},
     *      description="Get all UserUploads",
     *      produces={"application/json"},
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  @SWG\Items(ref="#/definitions/UserUpload")
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request)
    {
        $this->userUploadRepository->pushCriteria(new RequestCriteria($request));
        $this->userUploadRepository->pushCriteria(new LimitOffsetCriteria($request));
        $userUploads = $this->userUploadRepository->all();

        return $this->sendResponse($userUploads->toArray(), trans('flash.api.retrieve_success', ['model' => 'User Uploads']));
    }

    /**
     * @param CreateUserUploadAPIRequest $request
     * @return Response
     *
     * @SWG\Post(
     *      path="/userUploads",
     *      summary="Store a newly created UserUpload in storage",
     *      tags={"UserUpload"},
     *      description="Store UserUpload",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="UserUpload that should be stored",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/UserUpload")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/UserUpload"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function store(CreateUserUploadAPIRequest $request)
    {
        $res['path'] = $request->file('files')->store('files', 'upload');
        $res['url'] = fileUrl($res['path']);

        return $this->sendResponse($res, trans('flash.api.save_success', ['model' => 'User Upload']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Get(
     *      path="/userUploads/{id}",
     *      summary="Display the specified UserUpload",
     *      tags={"UserUpload"},
     *      description="Get UserUpload",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of UserUpload",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/UserUpload"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function show($id)
    {
        /** @var UserUpload $userUpload */
        $userUpload = $this->userUploadRepository->findWithoutFail($id);

        if (empty($userUpload)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'User Upload']), 404);
        }

        return $this->sendResponse($userUpload->toArray(), trans('flash.api.retrieve_success', ['model' => 'User Upload']));
    }

    /**
     * @param int $id
     * @param UpdateUserUploadAPIRequest $request
     * @return Response
     *
     * @SWG\Put(
     *      path="/userUploads/{id}",
     *      summary="Update the specified UserUpload in storage",
     *      tags={"UserUpload"},
     *      description="Update UserUpload",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of UserUpload",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Parameter(
     *          name="body",
     *          in="body",
     *          description="UserUpload that should be updated",
     *          required=false,
     *          @SWG\Schema(ref="#/definitions/UserUpload")
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  ref="#/definitions/UserUpload"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function update($id, UpdateUserUploadAPIRequest $request)
    {
        $input = $request->all();

        /** @var UserUpload $userUpload */
        $userUpload = $this->userUploadRepository->findWithoutFail($id);

        if (empty($userUpload)) {
            return $this->sendError(trans('flash.api.not_found', ['model'=>'User Upload']), 404);
        }

        $userUpload = $this->userUploadRepository->update($input, $id);

        return $this->sendResponse($userUpload->toArray(), trans('flash.api.update_success', ['model' => 'UserUpload']));
    }

    /**
     * @param int $id
     * @return Response
     *
     * @SWG\Delete(
     *      path="/userUploads/{id}",
     *      summary="Remove the specified UserUpload from storage",
     *      tags={"UserUpload"},
     *      description="Delete UserUpload",
     *      produces={"application/json"},
     *      @SWG\Parameter(
     *          name="id",
     *          description="id of UserUpload",
     *          type="integer",
     *          required=true,
     *          in="path"
     *      ),
     *      @SWG\Response(
     *          response=200,
     *          description="successful operation",
     *          @SWG\Schema(
     *              type="object",
     *              @SWG\Property(
     *                  property="success",
     *                  type="boolean"
     *              ),
     *              @SWG\Property(
     *                  property="data",
     *                  type="string"
     *              ),
     *              @SWG\Property(
     *                  property="message",
     *                  type="string"
     *              )
     *          )
     *      )
     * )
     */
    public function destroy($id)
    {
        /** @var UserUpload $userUpload */
        $userUpload = $this->userUploadRepository->findWithoutFail($id);

        if (empty($userUpload)) {
            return $this->sendError('User Upload not found', 404);
        }

        $userUpload->delete();

        return $this->sendResponse($id, trans('flash.api.delete_success', ['model' => 'User Upload']));
    }
}
